package com.hinacom.pix.tls;

import org.apache.commons.httpclient.protocol.Protocol;

import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
 * Created by fyu on 2016/11/7.
 */
public class TLSChennel {
    private static boolean isRegisterHttps = false;
    public synchronized static void registerHttps(String configFile) {
        if (isRegisterHttps == false) {
            ResourceBundle bundle = PropertyResourceBundle.getBundle(configFile);
            String trustStore;
            String trustStorePassword;
            String keyStore;
            String keyStorePassword;

            if (bundle.containsKey("tls.allow") && bundle.getString("tls.allow").equals("true")) {
                if (bundle.containsKey("trustStore")
                        && bundle.containsKey("trustStorePassword")) {
                    trustStore = bundle.getString("trustStore");
                    trustStorePassword = bundle.getString("trustStorePassword");

                    ClientSocketFactory clientSocketFactory = null;
                    if (bundle.containsKey("keyStore")
                            && bundle.containsKey("keyStorePassword")) {
                        keyStore = bundle.getString("keyStore");
                        keyStorePassword = bundle.getString("keyStorePassword");
                        clientSocketFactory = new ClientSocketFactory(keyStore, keyStorePassword, trustStore, trustStorePassword);
                    } else {
                        clientSocketFactory = new ClientSocketFactory(trustStore, trustStorePassword);
                    }
                    Protocol.registerProtocol("https", new Protocol("https", clientSocketFactory, 8011));



                }
            }

            isRegisterHttps = true;
        }
    }
}
